﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ControlFocusWin.Common;
using ControlFocusWin.WinUtilApi;

namespace ControlFocusWin.Biz
{
    public class WindowBiz
    {
        /// <summary>
        /// 获取指定窗口
        /// </summary>
        /// <param name="appName">应用名称</param>
        /// <param name="parentHwnd">0=应用顶级窗口; 0 != 子窗口</param>
        /// <returns></returns>
        public static IntPtr FindWinByName(string appName, IntPtr parentHwnd)
        {
            IntPtr winHwnd = IntPtr.Zero;
            if (parentHwnd == IntPtr.Zero)
            {
                winHwnd = Win32WindowApi.FindWindow(null, appName);
            }
            else
            {
                winHwnd = Win32WindowApi.FindWindowEx(parentHwnd, IntPtr.Zero, null, appName);   //获得按钮的句柄 
            }

            return winHwnd != IntPtr.Zero ? winHwnd : (IntPtr)(-1);
        }
         
        public static IntPtr FindWinByPoint(IntPtr parentHwnd, PixelPoint pt)
        {
            //IntPtr hwnd = Win32WindowApi.ChildWindowFromPointEx(parentHwnd, pt, ChildFromPointFlags.CWP_SKIPDISABLED|ChildFromPointFlags.CWP_SKIPTRANSPARENT);
            IntPtr hWnd = Win32WindowApi.WindowFromPoint(pt);
            return hWnd;
        }
         
        public static string GetClassName(IntPtr hwnd)
        {
            var sb = new StringBuilder(300);

            int res = Win32WindowApi.GetClassName(hwnd, sb, sb.Length);

            return sb.ToString();
        }

        public static string GetWinText(IntPtr hwnd)
        {
            var sb = new StringBuilder(300);
            Win32WindowApi.GetWindowText(hwnd, sb, sb.Capacity);
             
            return sb.ToString();
        }

        public static void ActiveShowWin(IntPtr hwnd)
        {
            Win32WindowApi.SetActiveWindow(hwnd);
            Win32WindowApi.SetForegroundWindow(hwnd);
            Win32WindowApi.ShowWindow(hwnd, 1);
        }

        public static void SettingWinPosSize(IntPtr hwnd)
        {
            //Win32WindowApi.SetWindowPos(hwnd, (IntPtr)(-1), 0, 0, 0, 0, 0x0040 | 0x0001);
            Win32WindowApi.MoveWindow(hwnd, 0, 0, 700, 500, true);
        }
        
        public static void SendMsg(IntPtr hwnd)
        {
            const int BM_CLICK = 0xF5;
            Win32WindowApi.SendMessage(hwnd, BM_CLICK, 0, 0);
        }


        
        public List<string> GetAppNames()
        {  
            CALLBACK myCallBack = new CALLBACK(Report);
            Win32WindowApi.EnumWindows(myCallBack, IntPtr.Zero);

            return null;
        }

        public bool Report(IntPtr hwnd, int lparm)
        {
            int pHwnd = Win32WindowApi.GetParent(hwnd); 
            if (pHwnd == 0 && Win32WindowApi.IsWindowVisible(hwnd) && Win32WindowApi.IsWindowEnabled(hwnd))
            { 
                var sb = new StringBuilder(300);
                Win32WindowApi.GetWindowText(hwnd, sb, sb.Capacity);
                if (sb.Length != 0)
                {
                    return true;
                }

                //注意某些窗口没有标题
                //if (sb.ToString() != String.Empty)
                //{
                //    if (sb.ToString()=="DemoWin")
                //    {
                //        int pId = GetParent(hwnd);
                //        //displayLabel.Text = sb.ToString();
                //        SetForegroundWindow(hwnd);
                //        int tmpId = SetActiveWindow(hwnd);
                //        //ShowWindow(hwnd, 3);
                //        Console.WriteLine(pId.ToString());

                //        //winListBox.Items.Add(sb.ToString());
                //    }

                //    winListBox.Items.Add(sb.ToString());
                //}
            }

            //回调函数有返回值
            return true;
        }


    }
}
